//
//  QuestionCell.m
//  ToYourHeartNews
//
//  Created by XJing on 16/1/22.
//  Copyright © 2016年 www.lanou3g.com 蓝鸥. All rights reserved.
//

#import "QuestionCell.h"
@implementation QuestionCell
- (void)dealloc
{
    [_askerImg release];
    [_askerLabel release];
    [_expertImg release];
    [_expertLable release];
    [_questionLabel release];
    [_answerLable release];
    [_qcontent release];
    [_acontent release];
    [super dealloc];
}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.askerImg = [[UIImageView alloc]init];
//        _askerImg.backgroundColor = [UIColor redColor];
        [self.contentView addSubview:_askerImg];
        [_askerImg release];
        
        self.askerLabel = [[UILabel alloc]init];
//        _askerLabel.backgroundColor = [UIColor orangeColor];
        [self.contentView addSubview:_askerLabel];
        [_askerLabel release];
        
        self.expertImg = [[UIImageView alloc]init];
//        _expertImg.backgroundColor = [UIColor greenColor];
        [self.contentView addSubview:_expertImg];
        [_expertImg release];
        
        self.expertLable = [[UILabel alloc]init];
//        _expertLable.backgroundColor = [UIColor blueColor];
        [self.contentView addSubview:_expertLable];
        [_expertLable release];
        
        self.questionLabel = [[UILabel alloc]init];
//        _questionLabel.backgroundColor = [UIColor magentaColor];
        _questionLabel.textColor = [UIColor grayColor];
        _questionLabel.numberOfLines = 0;
        [self.contentView addSubview:_questionLabel];
        [_questionLabel release];
        
        self.answerLable = [[UILabel alloc]init];
//        _answerLable.backgroundColor = [UIColor cyanColor];
        _answerLable.numberOfLines = 0;
        [self.contentView addSubview:_answerLable];
        [_answerLable release];
    }
    return self;
}
-(void)layoutSubviews
{
    [super layoutSubviews];
    _askerImg.frame = CGRectMake(5, 10, self.contentView.frame.size.width / 8, self.contentView.frame.size.width / 8);
    _askerImg.layer.cornerRadius = self.contentView.frame.size.width / 16;
    _askerImg.clipsToBounds = YES;
    
    _askerLabel.frame = CGRectMake(_askerImg.frame.origin.x + _askerImg.frame.size.width + 4, _askerImg.frame.origin.y, self.frame.size.width / 2, _askerImg.frame.size.height);
    _askerLabel.font = [UIFont systemFontOfSize:14];
    _askerLabel.textColor = [UIColor grayColor];
    //问题内容 需要自定义高度
    CGFloat questionH = [[self class] heightWithText:_qcontent withWidth:self.frame.size.width - _askerLabel.frame.origin.x - 10];
    _questionLabel.frame = CGRectMake(_askerLabel.frame.origin.x, _askerLabel.frame.origin.y + _askerLabel.frame.size.height + 5, self.frame.size.width - _askerLabel.frame.origin.x - 10, questionH);
    UILabel *line = [[UILabel alloc]initWithFrame:CGRectMake(_questionLabel.frame.origin.x, _questionLabel.frame.origin.y + _questionLabel.frame.size.height + 5, _questionLabel.frame.size.width, 5)];
//    line.backgroundColor = [UIColor yellowColor];
    line.text = @".......................";
    [self.contentView addSubview:line];
    //回答
    _expertImg.frame = CGRectMake(_askerImg.frame.origin.x, line.frame.origin.y + line.frame.size.height + 5, _askerImg.frame.size.width, _askerImg.frame.size.height);
    _expertImg.layer.cornerRadius = self.contentView.frame.size.width / 16;
    _expertImg.clipsToBounds = YES;
    
    _expertLable.frame = CGRectMake(_askerLabel.frame.origin.x, _expertImg.frame.origin.y, _askerLabel.frame.size.width, _askerLabel.frame.size.height);
    CGFloat answerH = [[self class] heightWithText:_acontent withWidth:self.frame.size.width - _askerLabel.frame.origin.x - 10];
    _answerLable.frame = CGRectMake(_questionLabel.frame.origin.x, _expertLable.frame.origin.y + _expertLable.frame.size.height + 5, _questionLabel.frame.size.width, answerH);
}
+ (CGFloat)heightWithText:(NSString *)text withWidth:(CGFloat )width
{
    //在类方法里self代表当前的类
    //写一个字典 保存文本属性(大小)
    NSDictionary *dic = @{NSFontAttributeName: [UIFont systemFontOfSize:17]};
    //预设一个尺寸 表示文本最大不超过这个尺寸
    //根据设置的宽度计算高度(宽度和lable保持一致)
    CGSize size = CGSizeMake(width , 1000);
    //根据文本 计算出一个CGRect
    //参数1: 尺寸范围
    //参数2: 文本属性
    //参数3: 按照什么方式获取rect
    //参数4: nil
    CGRect rect = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    //返回得到的高度
    
    return rect.size.height;
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
